/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pokemon.web;

import com.pokemon.web.security.CustomUserDetails;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.Before;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.controller.LifecycleStage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * 
 * @author Branislav Paulis
 */
public class CustomActionBean implements ActionBean {

    final static Logger log = LoggerFactory.getLogger(CustomActionBean.class);
 
    private ActionBeanContext context;
    
    private CustomUserDetails user;
    
    @DefaultHandler
    public Resolution all() {
        log.debug("all()");
        return new ForwardResolution("/index.jsp");
    }
    
    @Override
    public void setContext(ActionBeanContext context) {
        this.context = context;
    }
    
    @Override
    public ActionBeanContext getContext() {
        return context;
    }
    
    public void setUser(CustomUserDetails user) {
        this.user = user;
    }
    
    public CustomUserDetails getUser() {
        return user;
    }
    
    @Before(stages = LifecycleStage.BindingAndValidation)
    public void loadUser() {
        if (SecurityContextHolder.getContext().getAuthentication() != null) {
            Object object = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            log.debug("loaded principal: {}", object);
            if (object instanceof CustomUserDetails) {
                user = (CustomUserDetails) object;
            }
        }
        log.debug("loaded user: {}", user);
    }
}